<template>
  <base-breadcrumb :title="titleName">
    <searchData
      :search-data="searchArr"
      @search="search"
    />
    <a-card :bordered="false">
      <title-name title="分包商年度定级列表" />
      <div class="pub-table-select-count">
        <a-icon
          type="info-circle"
          theme="twoTone"
        />
        <span style="margin-right: 12px"> 评级分包商：<span class="totalNum">{{ totalDels.total }} 个</span> | 待评级：<span class="totalNum">{{ totalDels.unratedNum }} 个</span> | 已评级：<span
            class="totalNum">{{ totalDels.ratedNum }} 个</span></span>
      </div>
      <a-table
        :loading="loadingState.isTable"
        :pagination="false"
        bordered
        :scroll="{x:2400}"
        :columns="columns"
        :data-source="datas"
        :custom-row="selectRow"
      >
        <span
          slot="serial"
          slot-scope="text, record, index"
        >{{ index + 1 }}</span>
        <span
          slot="type"
          slot-scope="text, record, index"
        >
          <ellipsis
            :length="40"
            tooltip
          >{{ text }}</ellipsis>
        </span>

        <span
          slot="branchAaverage"
          slot-scope="text, record,index"
        >
          <span v-if="record.branchAaverage == null">无</span>
          <span v-if="record.branchAaverage == null">{{ record.branchAaverage }}</span>
        </span>

        <span
          slot="projectAverage"
          slot-scope="text, record,index"
        >
          <span v-if="record.projectAverage == null">无</span>
          <span v-if="record.projectAverage == null">{{ record.projectAverage }}</span>
        </span>

        <span
          slot="contractAmount"
          slot-scope="text, record"
        >
          <span>
            <BaseInputNumber
              :disabled="disabled"
              :value="record.contractAmount"
              @change="handleChangeContractAmount"
            />
          </span>
        </span>
        <span
          slot="outputValue"
          slot-scope="text, record"
        >
          <span>
            <BaseInputNumber
              :disabled="disabled"
              :value="record.outputValue"
              @change="handleChangeOutputValue"
            />
          </span>
        </span>
        <span
          slot="yearLevel"
          slot-scope="text, record"
        >
          <a-select
            :disabled="disabled"
            style="width: 100%;"
            :value="record.yearLevel"
            @change="handleChangeLevel"
          >
            <a-select-option
              v-for="item in levels"
              :key="item.id"
              :value="item.id"
            >{{ item.name }}</a-select-option>
          </a-select>
        </span>
        <span
          slot="action"
          slot-scope="text, record"
        >
          <template v-if=" type != 'view' ">
            <!--<a @click="handleDels(record)"  >查看</a>-->
            <!--<a-divider type="vertical"/>-->
            <a
              :disabled="disabled"
              @click="handleEdit(record)"
            >保存评级</a>
          </template>
        </span>
      </a-table>
      <pagination
        v-model="pageNo"
        :total="totalSize"
        :page-size.sync="pageSize"
        @change="handlePageChange"
        @showSizeChange="showSizeChange"
      />
    </a-card>

    <div class="page-btn-right-top">
      <!-- <div style="float:right;margin-top: 20px"> -->
      <template v-if="!disabled">
        <a-button
          type="primary"
          style="margin-right: 20px"
          @click="saveDraft"
        >
          保存草稿
        </a-button>
        <a-button
          type="primary"
          style="margin-right: 20px"
          @click="submit"
        >
          提交
        </a-button>
      </template>
      <a-button
        type="primary"
        @click="goBack"
      >
        关闭
      </a-button>
    </div>

    <a-modal
      width="1000px"
      :visible="visible"
      title="分包商信息"
      @cancel="visible = false"
      @ok="visible = false"
    >
      <title-name title="分包商信息" />
      <a-form-model
        :label-col="{ span: 5 }"
        :wrapper-col="{ span: 12 }"
        :model="form"
      >
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="分包商名称">
              <BaseInput
                v-model="form.name"
                disabled
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="分包商类型">
              <BaseInput
                v-model="form.name"
                disabled
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="分公司">
              <BaseInput
                v-model="form.name"
                disabled
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="在施项目名称">
              <BaseInput
                v-model="form.name"
                disabled
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="合作年限">
              <BaseInput
                v-model="form.name"
                disabled
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="当年分公司考核平均分">
              <BaseInput
                v-model="form.name"
                disabled
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="当年项目部考核平均分">
              <BaseInput
                v-model="form.name"
                disabled
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="上年度评级">
              <BaseInput
                v-model="form.name"
                disabled
              />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
      <title-name title="分包商当年合作信息" />
      <a-table
        bordered
        row-key="id"
        :columns="columnsItem"
        :data-source="dataItem"
      >
        <span
          slot="serial"
          slot-scope="text, record, index"
        >{{ index + 1 }}</span>
      </a-table>
    </a-modal>
    <!-- 选择审批人 -->
    <selectApprover
      :business-id="businessId"
      :process-config="setProcessConfig"
    />
    <!-- 选择审批人 -->
  </base-breadcrumb>
</template>

<script>
import moment from 'moment'
import { STable, Ellipsis } from '@/components'
import { getAnnualGradingList, getGradingTotal, editAnnualGrading, addGradingDraft, addGradingReview, getProjectCommon } from '@/api/subcontractors/annualRating'
import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司
import OrgTreeSelect from '@/components/OrgTreeSelect'

import ARow from 'ant-design-vue/es/grid/Row'

const columns = [
  {
    title: '序号',
    width: '65px',
    scopedSlots: { customRender: 'serial' }
  },
  {
    title: '评级年份',
    dataIndex: 'reviewYear'
  },
  {
    title: '分包商名称',
    dataIndex: 'subcontractorName'
  },
  {
    title: '分包商类型',
    dataIndex: 'type',
    scopedSlots: { customRender: 'type' }
  },
  {
    title: '分公司',
    dataIndex: 'branch'
  },
  {
    title: '在施项目名称',
    dataIndex: 'projectName'
  },
  {
    title: '合作年限',
    dataIndex: 'cooperation'
  },
  {
    title: '分公司考核平均分',
    dataIndex: 'branchAaverage',
    scopedSlots: { customRender: 'branchAaverage' }
  },
  {
    title: '项目部考核平均分',
    dataIndex: 'projectAverage',
    scopedSlots: { customRender: 'projectAverage' }
  },
  {
    title: '合同额(万)',
    dataIndex: 'contractAmount',
    scopedSlots: { customRender: 'contractAmount' }
  },
  {
    title: '年施工分包产值(万)',
    dataIndex: 'outputValue',
    scopedSlots: { customRender: 'outputValue' }
  },
  {
    title: '上年度评级',
    dataIndex: 'lastLevel'
  },
  {
    title: '本年度评级',
    dataIndex: 'yearLevel',
    scopedSlots: { customRender: 'yearLevel' }
  },
  {
    title: '本年度评级状态',
    dataIndex: 'statusName'
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: '150px',
    fixed: 'right',
    scopedSlots: { customRender: 'action' }
  }
]
const columnsItem = [
  {
    title: '序号',
    scopedSlots: { customRender: 'serial' }
  },
  {
    title: '合作项目名称',
    dataIndex: 'reviewYear'
  },
  {
    title: '项目所在分公司',
    dataIndex: 'subcontractorName'
  },
  {
    title: '合作时间',
    dataIndex: 'yearLevel'
  },
  {
    title: '综合评分',
    dataIndex: 'statusName'
  },
  {
    title: '项目部考核平均分',
    dataIndex: 'statusName1'
  },
  {
    title: '分公司考核平均分',
    dataIndex: 'statusName2'
  }
]

export default {
  name: 'a' + Date.now(),
  components: {
    ARow,
    Ellipsis
  },
  data() {
    this.columns = columns
    this.columnsItem = columnsItem
    return {
      loadingState: {
        isTable: false
      },
      totalSize: 0,
      totalPage: 0,
      pageNo: 1,
      pageSize: 10,

      form: {},
      visible: false,
      levels: [],
      searchArr: [
        // 搜索框
        {
          name: '分包商名称',
          value: 'subcontractorName',
          type: 'input'
        },
        {
          name: '本年评级状态',
          value: 'statusCode',
          type: 'select',
          data: [
            {
              value: '0',
              label: '待评级'
            },
            {
              value: '1',
              label: '已评级'
            }
          ]
        },
        {
          name: '分公司',
          valueKey: 'branchId',
          type: SelectSubCompany
        },
        {
          name: '项目',
          value: 'projectName',
          type: 'input'
        },
        {
          name: '本年度评级',
          value: 'annuaLevel',
          type: 'select',
          data: []
        }
      ],
      titleName: '',
      datas: [],
      dataItem: [],
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {
        pageNo: 1,
        pageSize: 20
      },
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        return getAnnualGradingList(requestParameters).then((res) => {
          return res
        })
      },
      selectedRowKeys: [],
      selectedRows: [],
      selectedRow: {}, // 当前行数据
      totalDels: {},
      id: '', // 分包商年度评级id
      // 流程配置
      processConfig: null,
      //点击提交发送流程配置值
      setProcessConfig: null,
      // 业务id
      businessId: null
    }
  },
  async created() {
    this.id = this.$route.query.id
    this.queryParam.id = this.$route.query.id
    this.type = this.$route.query.type
    this.getProjectCommon()
    this.getGradingTotal()
    this.getAnnualGradingList()
    //获取通过获取流程配置值
    this.processConfig = await this.$getConfigFn('labor_subcontract_annual_rating')
  },
  computed: {
    rowSelection() {
      return {
        selectedRowKeys: this.selectedRowKeys,
        selectedRows: this.selectedRows, // 当前行所有数据
        onChange: this.onSelectChange
      }
    },
    disabled() {
      return this.type === 'view'
    }
  },
  methods: {
    handlePageChange(pageNo, pageSize) {
      this.pageNo = pageNo
      this.getAnnualGradingList()
    },
    showSizeChange(pageNo, pageSize) {
      this.pageNo = 1
      this.pageSize = pageSize
      this.getAnnualGradingList()
    },

    getProjectCommon() {
      getProjectCommon({ type: 14, enabledCode:1 }).then((res) => {
        this.levels = res.data
        this.searchArr[4].data = this.getChangeDatatTypes(res.data)
      })
    },
    goBack() {
      this.$multiTab.close(this.$route.fullpath)
    },
    search(val) {
      this.pageNo = 1
      this.queryParam = val
      this.getAnnualGradingList()
      this.getGradingTotal()
    },

    saveDraft() {
      // 保存草稿
      addGradingDraft({ id: this.id }).then((res) => {
        if (res.code == 200 || res.data.code == 200) {
          this.$message.success('保存草稿成功')
          this.$multiTab.close(this.$route.fullpath)
        }
      })
    },

    submit() {
      let _this = this
      this.setProcessConfig = null
      addGradingReview({ id: _this.id }).then((res) => {
        if (res.code == 200 || res.data.code == 200) {
          _this.$message.success('提交审核成功！')
          _this.businessId = _this.$route.query.id
          _this.setProcessConfig = _this.processConfig
        }
      })
    },

    getAnnualGradingList() {
      this.queryParam.id = this.id
      this.queryParam.pageNo = this.pageNo
      this.queryParam.pageSize = this.pageSize
      this.loadingState.isTable = true
      getAnnualGradingList(this.queryParam)
        .then((res) => {
          this.loadingState.isTable = false
          this.datas = res.data
          this.totalSize = res.totalSize
          this.totalPage = res.totalPage
        })
        .catch(() => {
          this.loadingState.isTable = false
        })
    },
    selectRow(record) {
      return {
        on: {
          click: () => {
            this.selectedRow = record // 当前行数据
          }
        }
      }
    },
    handleChangeContractAmount(val) {
      // 合同额
      this.selectedRow.contractAmount = val
    },
    handleChangeOutputValue(val) {
      // 年施工分包产值
      this.selectedRow.outputValue = val
    },
    handleChangeLevel(val) {
      // 本年度评级
      this.selectedRow.yearLevel = val
    },

    getGradingTotal() {
      this.queryParam.id = this.id
      getGradingTotal(this.queryParam).then((res) => {
        this.totalDels = res.data
      })
    },

    handleDels(val) {
      this.visible = true
    },

    handleEdit() {
      // 保存评级
      let obj = {}
      obj.contractAmount = this.selectedRow.contractAmount
      obj.id = this.selectedRow.id
      obj.outputValue = this.selectedRow.outputValue
      obj.yearLevel = this.selectedRow.yearLevel

      if (this.selectedRow.yearLevel == null) {
        this.$message.error('请先选择年度评级！')
        return
      }

      editAnnualGrading(obj).then((res) => {
        if (res.code == 200) {
          this.$message.success('保存评级成功')
          this.getAnnualGradingList()
          this.getGradingTotal()
        }
      })
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRow = selectedRows
    }
  }
}
</script>


